package com.ewzj.bigdata.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.di.component.DaggerCollectUpdateAndCreateComponent;
import com.ewzj.bigdata.di.module.CollectUpdateAndCreateModule;
import com.ewzj.bigdata.mvp.contract.CollectUpdateAndCreateContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.CollectBean;
import com.ewzj.bigdata.mvp.presenter.CollectUpdateAndCreatePresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class CollectUpdateAndCreateActivity extends BaseActivity<CollectUpdateAndCreatePresenter> implements CollectUpdateAndCreateContract.View {


    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    private String title;
    private CollectBean collectBean;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerCollectUpdateAndCreateComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .collectUpdateAndCreateModule(new CollectUpdateAndCreateModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_collectupdateandcreate; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        tvConfirm.setVisibility(View.VISIBLE);
        title=getIntent().getStringExtra(AppConstants.EXTRA_DATA_STRING);
        collectBean=getIntent().getParcelableExtra(AppConstants.EXTRA_DATA);
        if (!TextUtils.isEmpty(title)){
            toolbarTitle.setText(title);
        }

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        String str=etName.getText().toString();
        if (TextUtils.isEmpty(str)){
            ArmsUtils.snackbarText("收藏名称不能为空");
            return;
        }
        if ("创建分组".equals(title)){
            //创建分组
            mPresenter.addCollect(str);
        }else {
//            修改分组信息
            if (collectBean!=null){
                mPresenter.updateCollect(str,collectBean.getId()+"");
            }
        }
    }

    @Override
    public void confirmDone() {
        killMyself();
    }
}
